﻿using CommunityToolkit.Mvvm.ComponentModel;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;

namespace ElsCircuit.Model
{
    /// <summary>
    /// 
    /// </summary>
    public class circuitModel : ObservableObject
    {
        /// <summary>
        /// 是否需要购买
        /// </summary>
        private bool _IsNeedBuy;
        /// <summary>
        /// 是否需要购买
        /// </summary>
        public bool IsNeedBuy { get => _IsNeedBuy; set { SetProperty(ref _IsNeedBuy, value); Draw(); DataChangeEx(); } }
        /// <summary>
        /// 是否需要购买
        /// </summary>
        private bool _IsCopy;
        /// <summary>
        /// 是否需要购买
        /// </summary>
        public bool IsCopy
        {
            get => _IsCopy; set
            {
                if (value)
                {
                    if (CircuitType==EnumCircuitType.non)
                    {

                    }
                    else if (CircuitType == EnumCircuitType.T1 ||
                        CircuitType == EnumCircuitType.T2 ||
                        CircuitType == EnumCircuitType.T3 ||
                        CircuitType == EnumCircuitType.T4
                        )
                    {
                        
                    }
                    else
                    {
                        value = false;
                    }
                }
                SetProperty(ref _IsCopy, value); Draw();
                DataChangeEx();
            }
        }

        /// <summary>
        /// 类型
        /// </summary>
        private EnumCircuitType _CircuitType;
        /// <summary>
        /// 类型
        /// </summary>
        public EnumCircuitType CircuitType { get => _CircuitType; set => SetProperty(ref _CircuitType, value); }

        /// <summary>
        /// 颜色类型
        /// </summary>
        private int _colorType;
        /// <summary>
        /// 颜色类型
        /// </summary>
        public int colorType { get => _colorType; set => SetProperty(ref _colorType, value); }

        /// <summary>
        /// 路径
        /// </summary>
        [Newtonsoft.Json. JsonIgnore]
        private DrawingImage _pathData;
        /// <summary>
        /// 路径
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public DrawingImage pathData { get => _pathData; set => SetProperty(ref _pathData, value); }
        /// <summary>
        /// 序号
        /// </summary>
        public int index { get; set; }
        /// <summary>
        /// 组
        /// </summary>
        public int group { get; set; }
        public event Action DataChange;

        public void DataChangeEx()
        {
            DataChange?.Invoke();
        }
        public void Draw()
        {
            int width = 4;
            int size = 200;
            int start = size / 2 - (width / 2);
            GeometryDrawing drawing1 = new GeometryDrawing(
                    Brushes.Transparent,
                    null,
                    new RectangleGeometry(new Rect(0, 0, size, size))
                );

            GeometryGroup whiteRectangles = new GeometryGroup();
            //if (IsNeedBuy)
            //{

            //    FormattedText formattedText = new FormattedText(
            //    "Buy",               // 文本内容
            //    CultureInfo.CurrentCulture,    // 文本所使用的区域性信息
            //    FlowDirection.LeftToRight,     // 文本的流动方向
            //    new Typeface("微软雅黑"),         // 字体
            //    12,                            // 字号
            //    Brushes.Black                   // 字体颜色
            //    );
            //    whiteRectangles.Children.Add(formattedText.BuildGeometry(new Point(0,0)));
            //}
            //if (IsCopy)
            //{
            //    FormattedText formattedText = new FormattedText(
            //   "复",               // 文本内容
            //   CultureInfo.CurrentCulture,    // 文本所使用的区域性信息
            //   FlowDirection.LeftToRight,     // 文本的流动方向
            //   new Typeface("微软雅黑"),         // 字体
            //   12,                            // 字号
            //   Brushes.Black                   // 字体颜色
            //   );
            //    whiteRectangles.Children.Add(formattedText.BuildGeometry(new Point(0, 12)));
            //}
            switch (CircuitType)
            {
                case EnumCircuitType.non:
                    break;
                case EnumCircuitType.I1:
                    //-
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(0, size / 2 - (width / 2), size, width)));
                    break;
                case EnumCircuitType.I2:
                    //|
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(size / 2 - (width / 2), 0, width, size)));
                    break;
                case EnumCircuitType.L1:
                    //上i 左-
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(size / 2 - (width / 2), 0, width, size / 2)));
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(0, start, size / 2, width)));
                    break;
                case EnumCircuitType.L2:
                    //上i 右-
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(size / 2 - (width / 2), 0, width, size / 2)));
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(start, start, size / 2, width)));
                    break;
                case EnumCircuitType.L3:
                    //下i 右-
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(start, start, width, size / 2)));
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(start, start, size / 2, width)));
                    break;
                case EnumCircuitType.L4:
                    //下i 左-
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(start, start, width, size / 2)));
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(0, start, size / 2, width)));
                    break;
                case EnumCircuitType.T1:
                    //上
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(0, size / 2 - (width / 2), size, width)));
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(size / 2 - (width / 2), 0, width, size / 2)));
                    break;
                case EnumCircuitType.T2:
                    //下
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(0, size / 2 - (width / 2), size, width)));
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(start, start, width, size / 2)));
                    break;
                case EnumCircuitType.T3:
                    //左
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(size / 2 - (width / 2), 0, width, size)));
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(0, start, size / 2, width)));
                    break;
                case EnumCircuitType.T4:
                    //右
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(size / 2 - (width / 2), 0, width, size)));
                    whiteRectangles.Children.Add(new RectangleGeometry(new Rect(start, start, size / 2, width)));
                    break;

                default:
                    break;
            }
            GeometryDrawing drawing2 = new GeometryDrawing(
               Brushes.White,
               null,
               whiteRectangles
           );
            if (colorType == 0)
            {
                drawing2.Brush = Brushes.Red;
            }
            if (colorType == 1)
            {
                drawing2.Brush = Brushes.Blue;
            }
            if (colorType == 2)
            {
                drawing2.Brush = Brushes.Green;
            }
            DrawingGroup imageDrawings = new DrawingGroup();

            imageDrawings.Children.Add(drawing1);
            imageDrawings.Children.Add(drawing2);
            var pathData = new DrawingImage(imageDrawings);
            pathData.Freeze();
            this.pathData = pathData;
        }
    }
}
